{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python385jvsc74a57bd0ae7890921ac3c17143ff000ac7152addc6614e2051824da39aa37dab63d26d82",
   "display_name": "Python 3.8.5 64-bit ('base': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "#5大金刚 list str tuple dic set\n",
    "#list创建01\n",
    "names = ['zhangsan','lisi','wangwu']\n",
    "names"
   ],
   "cell_type": "code",
   "metadata": {},
   "execution_count": 1,
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "['zhangsan', 'lisi', 'wangwu']"
      ]
     },
     "metadata": {},
     "execution_count": 1
    }
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7]"
      ]
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "source": [
    "#list创建02\n",
    "names = list()#创建一个空的列表，所有iterable可遍历的都可以接受\n",
    "names = list(range(8))\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "names = list((1,2,3))\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "['moxi', 'love', 'aiyou']"
      ]
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "source": [
    "#list增加元素：append, insert, extend, +\n",
    "#增1\n",
    "names = []\n",
    "names.append('moxi')\n",
    "names.append('love')\n",
    "names.append('aiyou')\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "['for sure', 'moxi', 'love', 'aiyou']"
      ]
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "source": [
    "#增2 - 插队\n",
    "names.insert(0,'for sure')\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "['for sure', 'moxi', 'love', 'aiyou', 999]"
      ]
     },
     "metadata": {},
     "execution_count": 6
    }
   ],
   "source": [
    "#增3 - 插入的下标超出了长度，就放在最后\n",
    "names.insert(9,999)\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "['for sure', 'moxi', 999, 'love', 'aiyou', 999]"
      ]
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "source": [
    "names.insert(-3,999)\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "['for sure', 'moxi', 999, 'love', 'aiyou', 999, 'work', 'study']"
      ]
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "source": [
    "#增4 - 列表增加列表\n",
    "bad_guys = ['work','study']\n",
    "# names.append(bad_guys)\n",
    "names.extend(bad_guys)\n",
    "#names += bad_guys\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "['for sure', 'moxi', 'love', 'aiyou', 999, 'work', 'study']"
      ]
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "#list删除元素的3种方法：del,pop,remove\n",
    "#两种删除方法：根据下标删除，根据内容删除\n",
    "# 删1\n",
    "del names[2]\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "['for sure', 'moxi', 'love', 'aiyou', 999, 'work']"
      ]
     },
     "metadata": {},
     "execution_count": 10
    }
   ],
   "source": [
    "#删2：pop - \n",
    "# - 默认去掉最后一个\n",
    "# - 也可以指定下标\n",
    "# - pop相较于del是可以返回删除值的\n",
    "# - 如果下标越界，抛出IndexError\n",
    "names.pop()\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'love'"
      ]
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "source": [
    "names.pop(1)\n",
    "n = names.pop(1)\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "['for sure', 'aiyou', 999, 'work']\n"
     ]
    }
   ],
   "source": [
    "#删3:\n",
    "# - 只删除第一个符合条件的, 如果没有符合条件的，就出ValueErro\n",
    "if 'yuwen' in names:\n",
    "    names.remove('yuwen')\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "['Evin', 'aiyou', 999, 'work']"
      ]
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "source": [
    "#list改：修改元素值 - list 里面保存的是实际对象的地址，修改的过程就是指向了新的地址\n",
    "names[0] = 'Evin'\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "'Evin'"
      ]
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "source": [
    "#list查询\n",
    "names[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "metadata": {},
     "execution_count": 15
    }
   ],
   "source": [
    "#查下标\n",
    "names.index('aiyou')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "['Evin', 'aiyou', 999, 'work', 886]\n5\n"
     ]
    }
   ],
   "source": [
    "#可以用in,not in判断\n",
    "if(886 not in names):\n",
    "    names.append(886)\n",
    "else:\n",
    "    names.index(886)\n",
    "print(names)\n",
    "print(len(names))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "['Evin', 999, 886]"
      ]
     },
     "metadata": {},
     "execution_count": 17
    }
   ],
   "source": [
    "#list切片\n",
    "names[0:5:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "hello, Evin\nhello, aiyou\nhello, 999\nhello, work\nhello, 886\n"
     ]
    }
   ],
   "source": [
    "#操作：loop, +, *比较， sort, reverse, max, min, max, sum\n",
    "#操1:loop\n",
    "for n in names:\n",
    "    print('hello,',n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "no1 is Evin\nno2 is aiyou\nno3 is 999\nno4 is work\nno5 is 886\n"
     ]
    }
   ],
   "source": [
    "for index,n in enumerate(names):#enumerate函数返回一对数\n",
    "    print(f'no{index + 1} is {n}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "['Evin', 'aiyou', 999, 'work', 886, 87, 65, 98, 33]"
      ]
     },
     "metadata": {},
     "execution_count": 21
    }
   ],
   "source": [
    "#操2：基本运算\n",
    "scores = [87,65,98,33]\n",
    "scores2 = [87, 65, 98, 33]\n",
    "names+scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "['Evin', 'aiyou', 999, 'work', 886, 87, 65, 98, 33, 87, 65, 98, 33]"
      ]
     },
     "metadata": {},
     "execution_count": 23
    }
   ],
   "source": [
    "names.extend(scores)\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "['Evin',\n",
       " 'aiyou',\n",
       " 999,\n",
       " 'work',\n",
       " 886,\n",
       " 87,\n",
       " 65,\n",
       " 98,\n",
       " 33,\n",
       " 87,\n",
       " 65,\n",
       " 98,\n",
       " 33,\n",
       " 'Evin',\n",
       " 'aiyou',\n",
       " 999,\n",
       " 'work',\n",
       " 886,\n",
       " 87,\n",
       " 65,\n",
       " 98,\n",
       " 33,\n",
       " 87,\n",
       " 65,\n",
       " 98,\n",
       " 33,\n",
       " 'Evin',\n",
       " 'aiyou',\n",
       " 999,\n",
       " 'work',\n",
       " 886,\n",
       " 87,\n",
       " 65,\n",
       " 98,\n",
       " 33,\n",
       " 87,\n",
       " 65,\n",
       " 98,\n",
       " 33]"
      ]
     },
     "metadata": {},
     "execution_count": 24
    }
   ],
   "source": [
    "names*3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "metadata": {},
     "execution_count": 25
    }
   ],
   "source": [
    "scores == scores2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[87, 65, 98, 33]"
      ]
     },
     "metadata": {},
     "execution_count": 26
    }
   ],
   "source": [
    "#操3：排序：ordered  sorted\n",
    "# list.reverse() 会修改原列表\n",
    "#reversed(list) 不会修改原列表，会返回一个新的\n",
    "#sort同上\n",
    "#sort可以指定reverse=true\n",
    "#sort可以指定key\n",
    "scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[33, 65, 87, 98]"
      ]
     },
     "metadata": {},
     "execution_count": 27
    }
   ],
   "source": [
    "#不改变原来的，只是重新排列这个数组\n",
    "sorted(scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[98, 87, 65, 33]"
      ]
     },
     "metadata": {},
     "execution_count": 28
    }
   ],
   "source": [
    "sorted(scores,reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[87, 65, 98, 33]"
      ]
     },
     "metadata": {},
     "execution_count": 29
    }
   ],
   "source": [
    "scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[87, 65, 98, 33]"
      ]
     },
     "metadata": {},
     "execution_count": 31
    }
   ],
   "source": [
    "scores.reverse()\n",
    "scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[33, 65, 87, 98]"
      ]
     },
     "metadata": {},
     "execution_count": 33
    }
   ],
   "source": [
    "scores.sort()#默认从小到大排序\n",
    "scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[98, 87, 65, 33]"
      ]
     },
     "metadata": {},
     "execution_count": 34
    }
   ],
   "source": [
    "scores.sort(reverse=True)#从小到大排序\n",
    "scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[('yuwen', 25), ('zhangsan', 87)]"
      ]
     },
     "metadata": {},
     "execution_count": 36
    }
   ],
   "source": [
    "name_scores = [('zhangsan',87),('yuwen',25)]\n",
    "sorted(name_scores,key=lambda n:n[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[98, 87, 65, 33]"
      ]
     },
     "metadata": {},
     "execution_count": 37
    }
   ],
   "source": [
    "#字符串的比较，是1个个字符比较过去，按照字符顺序,如果前面字符都一样，长的那个算大 maishu maishu8\n",
    "scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "98"
      ]
     },
     "metadata": {},
     "execution_count": 38
    }
   ],
   "source": [
    "max(scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "33"
      ]
     },
     "metadata": {},
     "execution_count": 40
    }
   ],
   "source": [
    "min(scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "283"
      ]
     },
     "metadata": {},
     "execution_count": 41
    }
   ],
   "source": [
    "sum(scores)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "2629004965568\n2629022091264\n"
     ]
    }
   ],
   "source": [
    "#复：推导式comprehension，map, filter\n",
    "names2 = names[:]#复制names\n",
    "print(id(names))\n",
    "print(id(names2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]\n"
     ]
    }
   ],
   "source": [
    "nums = list(range(100))\n",
    "nums_3 = [n for n in nums if n%3==0]\n",
    "print(nums_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 128, 136, 144, 152, 160, 168, 176, 184, 192, 200, 208, 216, 224, 232, 240, 248, 256, 264, 272, 280, 288, 296, 304, 312, 320, 328, 336, 344, 352, 360, 368, 376, 384, 392, 400, 408, 416, 424, 432, 440, 448, 456, 464, 472, 480, 488, 496, 504, 512, 520, 528, 536, 544, 552, 560, 568, 576, 584, 592, 600, 608, 616, 624, 632, 640, 648, 656, 664, 672, 680, 688, 696, 704, 712, 720, 728, 736, 744, 752, 760, 768, 776, 784, 792]\n"
     ]
    }
   ],
   "source": [
    "print(list(map(lambda n:n*8,nums)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}